<template>
  <div class="table-custom">
    <el-table :data="tableData" v-bind="$attrs" border stripe>
      <el-table-column
        v-for="column in columns"
        :key="column.prop"
        :prop="column.prop"
        :label="column.label"
        :width="column.width"
        :title="column.title || column.label"
        :min-width="column.minWidth"
        :max-width="column.maxWidth"
        show-overflow-tooltip
      >
        <template #header>
          <span :title="column.title || column.label">{{ column.label }}</span>
        </template>
        <template #default="scope">
          <slot
            :name="column.prop"
            :row="scope.row"
            :rowIndex="scope.$index"
            :value="scope.row[column.prop]"
          >
            <template v-if="column.type === 'image'">
              <el-image
                :preview-teleported="true"
                :src="
                  column.serverUrl
                    ? column.serverUrl + scope.row[column.prop]
                    : scope.row[column.prop]
                "
                :preview-src-list="[
                  column.serverUrl
                    ? column.serverUrl + scope.row[column.prop]
                    : scope.row[column.prop],
                ]"
                fit="cover"
                :style="{
                  width: column.imageWidth || '55px',
                  height: column.imageHeight || '16px',
                }"
              >
                <template #error>
                  <el-empty class="image-empty" description="未上传底单照片" />
                </template>
              </el-image>
              <!-- </template> -->
            </template>
            <template v-else-if="column.formatter">
              {{ column.formatter(scope.row[column.prop], scope.row) }}
            </template>
            <template v-else>
              {{ scope.row[column.prop] }}
            </template>
          </slot>
        </template>
      </el-table-column>
      <el-table-column
        v-if="showOperations"
        label="操作"
        fixed="right"
        :width="operationWidth"
      >
        <template #default="scope">
          <slot
            name="operation"
            :row="scope"
            :$index="scope.$index"
            :handle-operation="handleOperation"
          >
            <el-button
              v-for="(operation, index) in operations"
              :key="index"
              :size="operation.size || 'small'"
              :type="operation.type"
              plain
              :link="operation.link"
              @click="handleOperation(operation.name, scope.row)"
            >
              {{ operation.label }}
            </el-button>
          </slot>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container" v-if="showPagination">
      <el-pagination
        :current-page="currentPage"
        :page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="total"
        :background="true"
        size="small"
        layout="total, sizes, prev, pager, next, jumper"
        @update:current-page="handleCurrentChange"
        @update:page-size="handleSizeChange"
      />
    </div>
  </div>
</template>

<script>
import { defineComponent, ref } from 'vue'

export default defineComponent({
  name: 'TableCustom',
  props: {
    // 表格数据
    tableData: {
      type: Array,
      required: true,
      default: () => [],
    },
    // 列配置
    columns: {
      type: Array,
      required: true,
      default: () => [],
    },
    // 是否显示操作列
    showOperations: {
      type: Boolean,
      default: true,
    },
    // 操作列宽度
    operationWidth: {
      type: [String, Number],
      default: 200,
    },
    // 操作按钮配置
    operations: {
      type: Array,
      default: () => [],
    },
    // 是否显示分页
    showPagination: {
      type: Boolean,
      default: false,
    },
    // 总条数
    total: {
      type: Number,
      default: 0,
    },
    // 当前页码
    currentPage: {
      type: Number,
      default: 1,
    },
    // // 每页条数
    // pageSize: {
    //   type: Number,
    //   default: 10
    // }
    rules: {
      type: Object,
      default: () => ({}),
    },
  },
  emits: [
    'operation',
    'update:currentPage',
    // 'update:pageSize',
    'size-change',
    'load-data',
  ],
  setup(props, { emit }) {
    const pageSize = ref(100)
    // 处理操作按钮点击
    const handleOperation = (operationName, row) => {
      emit('operation', { name: operationName, row })
    }

    // 处理每页条数变化
    const handleSizeChange = (val) => {
      // emit('update:pageSize', val)
      pageSize.value = val // 更新内部 pageSize
      emit('load-data', {
        currentPage: 1,
        pageSize: val,
      })
    }

    // 处理页码变化
    const handleCurrentChange = (val) => {
      emit('update:currentPage', val)
      emit('load-data', {
        currentPage: val,
        pageSize: pageSize.value,
      })
    }

    return {
      pageSize, // 暴露给模板使用
      handleOperation,
      handleSizeChange,
      handleCurrentChange,
    }
  },
})
</script>

<style lang="scss" scoped>
.table-custom {
  width: 100%;

  :deep .el-table__header {
    width: 100% !important;
  }
  .el-table :deep td {
    padding: 0;
  }
  :deep td .cell {
    width: auto !important;
    height: auto !important;
    min-width: 0;
    padding: 8px 12px;
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    .el-image {
      position: relative;
      top: 4px;
      left: 20px;
      transform: scale(2);
      border-radius: 2px;
      .image-empty {
        position: relative;
        top: -2px;
        padding: 0;
        height: 100%;
        .el-empty__image {
          display: none;
          width: 100%;
          height: 100%;
        }
        .el-empty__description {
          margin: 0;
          transform: scale(0.5);
          p {
            font-size: 13px;
            color: #606266;
            font-weight: bold;
          }
        }
      }
    }
  }

  .pagination-container {
    margin-top: 15px;
    display: flex;
    justify-content: flex-end;
  }

  .pagination {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
  }

  .el-button + .el-button {
    margin-left: 6px;
  }
}
</style>
